package com.lcn.netty.utils;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * curator客户端获取工具
 */
public class CuratorUtils {

    private static String host = "127.0.0.1:2181"; // 单节点/集群主机
    private static String namespace = "lcn-im"; // 命名空间(根节点)
    private static int connectionTimeoutMs = 30 * 1000; // 连接超时时间
    private static int sessionTimeoutMs = 3 * 1000; // 会话超时时间
    private static int baseSleepTimeMs = 2 * 1000; // 每次重试间隔时间
    private static int maxRetires = 3; // 最大重试次数


    private static CuratorFramework client;
    static {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetires);
        client = CuratorFrameworkFactory.builder()
                .connectString(host)
                .connectionTimeoutMs(connectionTimeoutMs)
                .sessionTimeoutMs(sessionTimeoutMs)
                .retryPolicy(retryPolicy)
                .namespace(namespace)
                .build();
        client.start(); // 启动client客户端
    }

    public static CuratorFramework getClient() {
        return client;
    }

}
